package leecode.二叉树;

/**
 * @author wangxi03 created on 2021/6/23 4:20 下午
 * @version v1.0
 * https://www.nowcoder.com/questionTerminal/7298353c24cc42e3bd5f0e0bd3d1d759
 */
public class MergeTrees {

    public static void main(String[] args) {
        TreeNode t1 = TreeNode.deserialize("1,null,null");
        TreeNode t2 = TreeNode.deserialize("1,2,3,4,5,6,7,null,null,null,null,null,null,null,null");

        MergeTrees obj = new MergeTrees();
        TreeNode treeNode = obj.mergeTrees(t1, t2);

        System.out.println(TreeNode.serialize(treeNode));
    }

    public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {
        // write code here
        if (t1 == null && t2 == null) {
            return null;
        }
        if (t1 == null && t2 != null) {
            return t2;
        }
        if (t1 != null && t2 == null) {
            return t1;
        }
        int rootVal = t1.val + t2.val;
        t1.val = rootVal;
        // 把t2的节点放到t1树上，t2树的节点对自己孩子节点的引用没有变
        t1.left = mergeTrees(t1.left, t2.left);
        t1.right = mergeTrees(t1.right, t2.right);
        return t1;
    }
}
